package test.cor.controller;

import java.util.ArrayList;

import test.cor.command.ICommand;
import test.cor.command.ReadScaleCommand;

public class CommandController{

	private ArrayList<ICommand> cmdList;
	private ArrayList<String> listOfCommands = new ArrayList<String>();
	
	public CommandController(){
		cmdList = new ArrayList<ICommand>();		
	}
	
	public void addCommand(ICommand cmd){
			/*First added chain is the last being executed which means 
			 * that it won't need any nextChains. The last executed 
			 * command will have a null nextCommand.
			 */
		
		cmdList.add(cmd);
			if (cmdList.size() == 1){
				//Do nothing
			} else {
				//Sets the next chain towards to the end of the chain
				cmdList.get(cmdList.size() - 1).setNextChain(cmdList.get(cmdList.size() - 2));
			}
	}

	public ArrayList<String> getListOfCommandsAvailable(){
		//Initializes the command list available
		if (listOfCommands.isEmpty()){
			for (int i = cmdList.size(); i > 0; i--){
				listOfCommands.add(cmdList.get(i-1).info());
			}
		}
		return listOfCommands;
	}
	
	public void executeCommand(String cmd){
		cmdList.get(cmdList.size() - 1).command(cmd);
	}
}
